package com.hdoit.apollo.logical.cargo.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.hdoit.apollo.logical.cargo.dao.ICargoDao;
import com.hdoit.apollo.logical.cargo.mapper.StandardPropertyUserMapper;
import com.hdoit.apollo.logical.cargo.model.Cargo;
import com.hdoit.apollo.logical.cargo.model.Collocation;
import com.hdoit.apollo.logical.cargo.model.StandardPropertyUser;
import com.hdoit.apollo.logical.cargo.model.StandardUser;
import com.hdoit.apollo.logical.cargo.service.ICargoService;
import com.hdoit.apollo.logical.cargo.service.ICollocationService;
import com.hdoit.apollo.logical.cargo.service.IStandardPropertyUserService;
import com.hdoit.apollo.logical.cargo.service.IStandardUserService;
import com.hdoit.apollo.logical.stock.model.Stock;
import com.hdoit.apollo.logical.stock.model.StockCollocation;
import com.hdoit.apollo.logical.stock.service.IStockCollocationService;
import com.hdoit.apollo.logical.stock.service.IStockService;
import com.hdoit.apollo.logical.viewmodel.cargo.CargoSearch;
import com.tennetcn.core.common.enums.pub.YesOrNoInteger;
import com.tennetcn.core.common.utils.CommonUtils;
import com.tennetcn.core.common.utils.DateUtils;
import com.tennetcn.data.dao.base.impl.SuperService;
import com.tennetcn.data.enums.ModelStatus;
import com.tennetcn.data.message.PagerModel;
/**
 * @author      humaoshu
 * @email       
 * @createtime  2018年03月31日 15:09:06
 * @comment
 */
@Component
public class CargoServiceImpl extends SuperService<Cargo> implements ICargoService{
	
	@Autowired
	private ICargoDao cargoDao;
	
	@Autowired
	private IStandardUserService standardUserService;
	
	@Autowired
	private IStandardPropertyUserService standardPropertyUserService;
	
	@Autowired
	private StandardPropertyUserMapper standardPropertyUserMapper;
	
	@Autowired
	private ICollocationService collocationService;
	
	@Autowired
	private IStockService stockService;
	
	@Autowired
	private IStockCollocationService stockCollocationService;

	@Override
	public boolean applyChange(Cargo cargo, List<StandardUser> standardUsers,List<StandardPropertyUser> standardPropertyUsers,List<Collocation> collocations) {
		
		Stock stock=new Stock();
		List<StockCollocation> stockCollocations=new ArrayList<StockCollocation>();
		
		if(super.applyChange(cargo)){
			if(ModelStatus.add.equals(cargo.getModelStatus())){
				 saveStandardUsers(cargo, standardUsers);
				 saveStandPropertyUsers(cargo,standardPropertyUsers);
				 saveCollocations(cargo,collocations);
				 saveStock(cargo,stock);
				 saveStockCollocations(stock,stockCollocations,collocations);
			}else {
				
				if (!StringUtils.isEmpty(standardUsers)&&standardUsers.size()>0){
					for (StandardUser standardUser : standardUsers) {
						StandardUser standardUsered=standardUserService.queryModelByCargoIdAndStandardId(cargo.getCargoId(),standardUser.getStandardId());
						System.err.println(standardUser.getStandardUserId()+"ko"+standardUser.getCargoId()+"zha"+standardUser.getStandardId());
						if (StringUtils.isEmpty(standardUsered)) {
							System.err.println(standardUser.getStandardUserId()+"怎么来的？");
							standardUser.setCargoId(cargo.getCargoId());
							if (StringUtils.isEmpty(standardUser.getShopId())) {
								standardUser.setShopId(cargo.getShopId());
							}
							standardUser.setCreateDate(DateUtils.getCurrent());
							standardUser.setDeleteMark(YesOrNoInteger.no);
							standardUser.setModelStatus(ModelStatus.add);	
						}else {
							System.err.println(standardUser.getStandardUserId()+"就这么来的？");
							
							if (StringUtils.isEmpty(standardUser.getShopId())) {
								standardUser.setShopId(cargo.getShopId());
							}
							
							standardUser.setModifyDate(DateUtils.getCurrent());
							standardUser.setDeleteMark(YesOrNoInteger.no);
							standardUser.setModelStatus(ModelStatus.update);	
						}
						
						
					}
						
				}					
			
				if (!StringUtils.isEmpty(standardPropertyUsers)&&standardPropertyUsers.size()>0){
					for (StandardPropertyUser standardPropertyUser : standardPropertyUsers) {
						if (StringUtils.isEmpty(standardPropertyUser.getPropertyId())) {
							StandardPropertyUser standardPropertyUsered=standardPropertyUserMapper.queryModelByStandardUserIdAndCargoIdAndName(standardPropertyUser.getStandardUserId(),cargo.getCargoId(),standardPropertyUser.getOtherPropertyName());
							if (!StringUtils.isEmpty(standardPropertyUsered)) {
								standardPropertyUser.setStandardPropertyUserId(CommonUtils.getKeyId());
								standardPropertyUser.setCreateDate(DateUtils.getCurrent());
								if (StringUtils.isEmpty(standardPropertyUser.getShopId())) {
									standardPropertyUser.setShopId(cargo.getShopId());
								}
								System.err.println("我执行");
								standardPropertyUser.setDeleteMark(YesOrNoInteger.no);
								standardPropertyUser.setModelStatus(ModelStatus.update);
								
							}
						}else if (StringUtils.isEmpty(standardPropertyUser.getOtherPropertyName())) {
							StandardPropertyUser standardPropertyUsered=standardPropertyUserMapper.queryModelByStandardUserIdAndCargoIdAndPropertyId(standardPropertyUser.getStandardUserId(),cargo.getCargoId(),standardPropertyUser.getPropertyId());
							if (!StringUtils.isEmpty(standardPropertyUsered)) {
								System.err.println("我只想");
								standardPropertyUser.setStandardPropertyUserId(CommonUtils.getKeyId());
								standardPropertyUser.setCreateDate(DateUtils.getCurrent());
								if (StringUtils.isEmpty(standardPropertyUser.getShopId())) {
									standardPropertyUser.setShopId(cargo.getShopId());
								}
								standardPropertyUser.setDeleteMark(YesOrNoInteger.no);
								standardPropertyUser.setModelStatus(ModelStatus.update);
								
							}
						} else {
							System.err.println("我来了");
							standardPropertyUser.setStandardPropertyUserId(CommonUtils.getKeyId());
							standardPropertyUser.setCreateDate(DateUtils.getCurrent());
							if (StringUtils.isEmpty(standardPropertyUser.getShopId())) {
								standardPropertyUser.setShopId(cargo.getShopId());
							}
							standardPropertyUser.setDeleteMark(YesOrNoInteger.no);
							standardPropertyUser.setModelStatus(ModelStatus.add);
							
						}
						
						
						
					}
						
				}
				
				if (!StringUtils.isEmpty(collocations)&&collocations.size()>0){
					for (Collocation collocation : collocations) {
						
						collocation.setCargoId(cargo.getCargoId());
						
						collocation.setPurchasePrice(collocation.getCostPrice());
						
			    		collocation.setCollocationNo(cargo.getCargoNo()+"-"+DateUtils.convertDate2String("mmss",DateUtils.getCurrent()));
						collocation.setCreateDate(DateUtils.getCurrent());
						collocation.setShopId(cargo.getShopId());
			    		collocation.setDeleteMark(YesOrNoInteger.no);
			    		if (!StringUtils.isEmpty(collocation.getCollocationId())) {
							
							Collocation collocationed=collocationService.queryModel(collocation.getCollocationId());
							if (!StringUtils.isEmpty(collocationed)) {
								collocation.setModelStatus(ModelStatus.update);
								
							}else {
								collocation.setModelStatus(ModelStatus.add);
								
							}
							
						}else {
							collocation.setCollocationId(CommonUtils.getKeyId());
							collocation.setModelStatus(ModelStatus.add);
							
						}
						
					}
						
				}

				Stock stocked=stockService.queryModelByCargoId(cargo.getCargoId());
				
				if (!StringUtils.isEmpty(collocations)&&collocations.size()>0) {
					for (Collocation collocation : collocations) {
						
						Double collocationDebt=0.0;
						
						if (collocation.getModelStatus().equals(ModelStatus.add)) {
							StockCollocation stockCollocation=new StockCollocation();
							stockCollocation.setStockCollocationId(CommonUtils.getKeyId());
							stockCollocation.setStockId(stocked.getStockId());
							stockCollocation.setCollocationId(collocation.getCollocationId());
							stockCollocation.setCollocationDebt(collocationDebt);
							stockCollocation.setCollocationNum(collocationDebt);
							stockCollocation.setCreateDate(DateUtils.getCurrent());
							stockCollocation.setDeleteMark(YesOrNoInteger.no);
							stockCollocation.setShopId(cargo.getShopId());
							stockCollocation.setModelStatus(ModelStatus.add);
							stockCollocations.add(stockCollocation);
						}
						
					
					}
				} 
				
				System.err.println(stockCollocations.size()+"分库存");
				
			}
			
			
			if(!StringUtils.isEmpty(standardUsers)&&standardUsers.size()>0){
				System.err.println(standardUsers.size()+"规格使用");
				List<StandardUser> sList=new ArrayList<StandardUser>();
				
				HashMap<String, StandardUser> map=new HashMap<String, StandardUser>();
				
				for (StandardUser standardUser : standardUsers) {
					String id=standardUser.getStandardId();
					if (!map.containsKey(id)) {
						map.put(id, standardUser);
					}
					
				}
				
				Iterator<Entry<String, StandardUser>> iter = map.entrySet().iterator();
				while (iter.hasNext()) {
					sList.add(iter.next().getValue());
				}
				
				standardUserService.applyChanges(sList);
			}
				
		
			if(!StringUtils.isEmpty(standardPropertyUsers)&&standardPropertyUsers.size()>0){
				System.err.println(standardPropertyUsers.size()+"属性使用");
				 standardPropertyUserService.applyChanges(standardPropertyUsers);		
			}
			
			
			if(!StringUtils.isEmpty(collocations)&&collocations.size()>0){
				System.err.println(collocations.size()+"价格使用");
				collocationService.applyChanges(collocations);
				
			}
			if(!StringUtils.isEmpty(stock.getStockId())){
				stockService.applyChange(stock);
			}
			
			if(!StringUtils.isEmpty(stockCollocations)&&stockCollocations.size()>0){
				stockCollocationService.applyChanges(stockCollocations);
			}
			
			return true;
			
		}
		return false;
	}
	
	public boolean saveStockCollocations(Stock stock,List<StockCollocation> stockCollocations,List<Collocation> collocations){
		for (Collocation collocation : collocations) {
			
			Double collocationDebt=0.0;
			
			StockCollocation stockCollocation=new StockCollocation();
			stockCollocation.setStockCollocationId(CommonUtils.getKeyId());
			stockCollocation.setStockId(stock.getStockId());
			stockCollocation.setCollocationId(collocation.getCollocationId());
			stockCollocation.setCollocationDebt(collocationDebt);
			stockCollocation.setCollocationNum(collocationDebt);
			stockCollocation.setCreateDate(DateUtils.getCurrent());
			stockCollocation.setDeleteMark(YesOrNoInteger.no);
			stockCollocation.setShopId(stock.getShopId());
			stockCollocation.setModelStatus(ModelStatus.add);
			stockCollocations.add(stockCollocation);
		}
		

		return true;
	}
	
	
	public boolean saveStock(Cargo cargo, Stock stock){
		Double stockNum=0.0;
		stock.setStockId(CommonUtils.getKeyId());
		stock.setShopId(cargo.getShopId());
		stock.setCargoId(cargo.getCargoId());
		stock.setStockNum(stockNum);
		stock.setStockDebt(stockNum);
		stock.setCreateDate(DateUtils.getCurrent());
		stock.setDeleteMark(YesOrNoInteger.no);
		stock.setModelStatus(ModelStatus.add);
		return true;
	}
	
	
	public boolean saveStandardUsers(Cargo cargo, List<StandardUser> standardUsers){
		for (StandardUser standardUser : standardUsers) {
			standardUser.setCargoId(cargo.getCargoId());
			standardUser.setShopId(cargo.getShopId());
			standardUser.setCreateDate(DateUtils.getCurrent());
			standardUser.setDeleteMark(YesOrNoInteger.no);
			standardUser.setModelStatus(ModelStatus.add);
		}
		
		return true;
	}

	
	public boolean saveCollocations(Cargo cargo,List<Collocation> collocations){
		for (Collocation collocation : collocations) {
			collocation.setCargoId(cargo.getCargoId());
			collocation.setCollocationId(CommonUtils.getKeyId());
			collocation.setPurchasePrice(collocation.getCostPrice());
			collocation.setCreateDate(DateUtils.getCurrent());
			collocation.setShopId(cargo.getShopId());
    		collocation.setCollocationNo(cargo.getCargoNo()+"-"+DateUtils.convertDate2String("mmss",DateUtils.getCurrent()));
    		collocation.setDeleteMark(YesOrNoInteger.no);
			collocation.setModelStatus(ModelStatus.add);
		}
	
		return true;
	}
	
	
	public boolean saveStandPropertyUsers(Cargo cargo,List<StandardPropertyUser> standardPropertyUsers){
		for (StandardPropertyUser standardPropertyUser : standardPropertyUsers) {
			standardPropertyUser.setStandardPropertyUserId(CommonUtils.getKeyId());
			standardPropertyUser.setCreateDate(DateUtils.getCurrent());
			standardPropertyUser.setDeleteMark(YesOrNoInteger.no);
			standardPropertyUser.setShopId(cargo.getShopId());
			standardPropertyUser.setModelStatus(ModelStatus.add);
		}

		return true;
	}

	@Override
	public Cargo getCargoByCargoNo(String cargoNo, String shopId) {
		return cargoDao.getCargoByCargoNo(cargoNo,shopId);
	}

	@Override
	public boolean checkCargoNo(String CargoId, String cargoNo, String shopId) {
		return cargoDao.checkCargoNo(CargoId,cargoNo,shopId);
	}

	public boolean saveCargos(List<Cargo> cargos, List<Stock> stocks,
			List<StandardUser> standardUsers,
			List<StandardPropertyUser> standardPropertyUsers,
			List<Collocation> collocations,
			List<StockCollocation> stockCollocations) {
		boolean result=false;
		result=super.applyChanges(cargos);
		if(result){
			result=standardUserService.applyChanges(standardUsers);
			if(result) {
				result=standardPropertyUserService.applyChanges(standardPropertyUsers);
				if(result) {
					result=collocationService.applyChanges(collocations);
					if(result) {
						result=stockService.applyChanges(stocks);
						if(result) {
							result=stockCollocationService.applyChanges(stockCollocations);
						}else {
							throw new RuntimeException();
						}
					}else {
						throw new RuntimeException();
					}
				}else {
					throw new RuntimeException();
				}
			}else {
				throw new RuntimeException();
			}
		}else {
			throw new RuntimeException();
		}
		return result;
	}

	@Override
	public boolean checkCargoNo(String cargoN) {
		return cargoDao.checkCargoNo(cargoN);
	}

	@Override
	public Cargo queryModelByCollocationId(String collocationId) {
		
		return cargoDao.queryModelByCollocationId(collocationId);
	}

	@Override
	public List<Cargo> queryListMPForPC(CargoSearch search,	PagerModel pagerModel) {
		
		return cargoDao.queryListMPForPC(search,pagerModel);
	}

	@Override
	public Cargo queryModelForView(String key) {
		
		return cargoDao.queryModelForView(key);
	}

	@Override
	public Cargo queryCargoById(String cargoId) {
		return cargoDao.queryCargoById(cargoId);
	}

	@Override
	public boolean deleteCargoById(String cargoId) {
		return cargoDao.deleteCargoById(cargoId);
	}

	@Override
	public Cargo getCargoByShapeCode(String shapeCode, String shopId) {
		return cargoDao.getCargoByShapeCode(shapeCode, shopId);
	}

	@Override
	public List<Cargo> queryListMPForWechat(CargoSearch search,
			PagerModel pagerModel) {
		
		return cargoDao.queryListMPForWechat(search,pagerModel);
	}

	
}